<?php
error_reporting(~E_ALL);

require_once("classes/functions.php");

$fixlatin = getFromInput("fixlatin");

echo getFromInput("delim");

$filename = getFromInput("fn");
if(!$filename){
	$filename = "export.xls";
}

$ext = getFileExtension($filename);

switch ($ext){
	case "xls":
		$ContentType="application/x-msexcel";
		break;
	default:
		$ContentType = "text/$ext";
		break;
}

$f = getFromInput("f");
if(file_exists($f)){
	header("content-type: $ContentType");
	header("content-disposition: attachment; filename=$filename");
	
	$content = file_get_contents($f);
	
	if(!$fixlatin && $fixlatin!=null){
		echo "fixed\r\n";
		var_export($fixlatin);
		fixLatin($content);
	}
	
	echo $content;
	//system("rm $f");
	unlink($f);
}else{
	echo '<script>alert("Fajl ne postoji.")</script>';
}

function fixLatin(&$str,$m=0){
	$mask = array(
		"č" => "c",
		"Č" => "C",
		"ć" => "c",
		"Ć" => "C",
		"ž" => "z",
		"Ž" => "Z",
		"đ" => "dj",
		"Đ" => "DJ",
		"š" => "s",
		"Š" => "S"
	);
	$mask1 = array(
		'ÄŚ' => "Č",
		'ÄŤ' => "č",
		'Ä†' => "Ć",
		'Ä‡' => "ć",
		'Ĺ ' => "Š",
		'Ĺˇ' => "š",
		'Ä' => "Đ",
		'Ä‘' => "đ",
		'Ĺ˝' => "Ž",
		'Ĺľ' => "ž"
	);
	$mask2 = array(
		"Č" => "╚",
		"č" => "Ŕ",
		"Ć" => "Ă",
		"ć" => "Š",
		"Š" => "Ő",
		"š" => "Ü",
		"Đ" => "đ",
		"đ" => "­",
		"Ž" => "Ä",
		"ž" => "×"
	);
	if($m==1){
		$mask = $mask1;
	}
	
	foreach ($mask as $k=>$v){
		$str = str_replace($k,$v,$str);
	}
}

?>